package com.simen.hellobytes.android.database;

import android.database.Cursor;

/**
 * author: SimenHi
 * create time: 2020-11-09 15:49
 * description:
 */
public class CursorWrapper {
    private Cursor cursor;

    public CursorWrapper(Cursor cursor) {
        this.cursor = cursor;
    }

    public boolean moveToLast(){
        if(cursor!=null){
            return cursor.moveToLast();
        }
        return false;
    }

    public boolean moveToFirst(){
        if(cursor!=null){
            return cursor.moveToFirst();
        }
        return false;
    }

    public boolean moveToNext(){
        if(cursor!=null){
            return cursor.moveToNext();
        }
        return false;
    }

    public boolean moveToPrevious(){
        if(cursor!=null){
            return cursor.moveToPrevious();
        }
        return false;
    }

    public boolean moveToPosition(int position){
        if(cursor!=null){
            return cursor.moveToPosition(position);
        }
        return false;
    }

    public boolean isFirst(){
        if(cursor!=null){
            return cursor.isFirst();
        }
        return false;
    }

    public boolean isLast(){
        if(cursor!=null){
            return cursor.isLast();
        }
        return false;
    }

    public boolean isClosed(){
        if(cursor!=null){
            return cursor.isClosed();
        }
        return true;
    }

    public void close(){
        if(cursor!=null){
            cursor.close();
        }
    }

    public int getType(int columnIndex){
        if(cursor!=null){
            return cursor.getType(columnIndex);
        }
        return Cursor.FIELD_TYPE_NULL;
    }

    public String getString(String columnName){
        return  getString(columnName,null);
    }

    public String getString(String columnName,String defVal){
        if(cursor!=null){
            int index=cursor.getColumnIndex(columnName);
            if(index!=-1){
                return cursor.getString(index);
            }
        }
        return defVal;
    }

    public int getInteger(String columnName){
        return  getInteger(columnName,0);
    }

    public int getInteger(String columnName,int defVal){
        if(cursor!=null){
            int index=cursor.getColumnIndex(columnName);
            if(index!=-1){
                return cursor.getInt(index);
            }
        }
        return defVal;
    }

    public long getLong(String columnName){
        return  getLong(columnName,0L);
    }

    public long getLong(String columnName,long defVal){
        if(cursor!=null){
            int index=cursor.getColumnIndex(columnName);
            if(index!=-1){
                return cursor.getLong(index);
            }
        }
        return defVal;
    }

    public byte[] getBlob(String columnName){
        return  getBlob(columnName,null);
    }

    public byte[] getBlob(String columnName,byte[] defVal){
        if(cursor!=null){
            int index=cursor.getColumnIndex(columnName);
            if(index!=-1){
                return cursor.getBlob(index);
            }
        }
        return defVal;
    }

    public double getDouble(String columnName){
        return  getDouble(columnName,0);
    }

    public double getDouble(String columnName,double defVal){
        if(cursor!=null){
            int index=cursor.getColumnIndex(columnName);
            if(index!=-1){
                return cursor.getDouble(index);
            }
        }
        return defVal;
    }

    public float getFloat(String columnName){
        return  getFloat(columnName,0);
    }

    public float getFloat(String columnName,float defVal){
        if(cursor!=null){
            int index=cursor.getColumnIndex(columnName);
            if(index!=-1){
                return cursor.getFloat(index);
            }
        }
        return defVal;
    }

    public short getShort(String columnName){
        return getShort(columnName, (short) 0);
    }

    public short getShort(String columnName,short defVal){
        if(cursor!=null){
            int index=cursor.getColumnIndex(columnName);
            if(index!=-1){
                return cursor.getShort(index);
            }
        }
        return defVal;
    }
}
